package com.duyj.base.config;


import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.lang.reflect.Type;

@Configuration
public class FeignConfig {

    @Bean
    public Decoder feignDecoder() {
        return new StringDecoder();
    }

    // 自定义解码器
    public static class StringDecoder implements Decoder {

        @Override
        public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
            if (type == String.class) {
                String body = Util.toString(response.body().asReader());
                // 去除两端的双引号
                return body.replaceAll("^\"|\"$", "");
            }
            return null;
        }

    }
}
